package model

import (
	"book-hub/book-service/basic/config"
	"gorm.io/gorm"
)

type Book struct {
	gorm.Model
	UserId  int     `gorm:"type:int(11);comment:管理员ID"`
	Name    string  `gorm:"type:varchar(30);comment:图书名称"`
	Status  string  `gorm:"type:varchar(30);comment:图书分类"`
	Author  string  `gorm:"type:varchar(30);comment:作者"`
	People  string  `gorm:"type:varchar(30);comment:译者"`
	Address string  `gorm:"type:varchar(30);comment:出版社"`
	Year    string  `gorm:"type:varchar(100);comment:出版时间"`
	Price   float64 `gorm:"type:decimal(10,2);comment:单价"`
	SjName  string  `gorm:"type:varchar(30);comment:书架名称"`
	Num     int     `gorm:"type:int(11);comment:库存"`
	Type    string  `gorm:"type:varchar(30);comment:状态"`
}

func CreateBook(b *Book) error {

	return config.DB.Create(&b).Error
}
func FindBook(b *Book, page int, size int) error {

	offset := (page - 1) * size
	return config.DB.Offset(offset).Limit(size).Find(&b).Error
}
func BookFind(b *Book, name string) error {

	return config.DB.Where("name like ?", "%"+name+"%").Find(&b).Error
}
func Update(b *Book, id int) error {

	return config.DB.Where("id=?", id).Updates(&b).Error
}
func DelBook(b *Book, id int) error {

	return config.DB.Where("id=?", id).Delete(&b).Error
}